#include "widget.h"
#include "./ui_widget.h"
#include "MyDevice.h"
#include "ReadThread.h"
#include<QDebug>
#include<QDesktopWidget>
#include<MyThread.h>
#include<QScreen>
#include <unistd.h>
//void QAudioOutput::setVolume(qreal volume); //设置音量 (0.0~1.0之间,默认音量为1.0)
//
//
//void QAudioOutput::setBufferSize(int value); //重新设置缓冲区大小,需要在start()之前设置
//
//int QAudioOutput::bufferSize() const； //获取缓冲区大小，默认为35280,需要start()后,才分配buff
//
//int QAudioOutput::bytesFree() //返回音频缓冲区中可用的空闲字节数。
////注意:返回值仅在QAudio::ActiveState或QAudio::IdleState状态下有效，否则返回零。
//
//
//int QAudioOutput::periodSize(); //周期大小(以字节为单位)，每播放一次音频数据所需多少个数据量
//　　　　　　　　　　　　　　　　　　　　//periodSize用来防止缓冲区欠运行和确保不间断回放所需的数据量








Widget::Widget(QWidget *parent)
        : QWidget(parent), ui(new Ui::Widget) {
    ui->setupUi(this);
//    socket = new QTcpSocket(this);
//
//    socket->connectToHost("127.0.0.1", 8080);
//    socket->waitForConnected();
    f = new QFile("/home/choumei/CLionProjects/NetClient/resource/audio.pcm");
    f->open(QIODevice::ReadOnly);
    format = new QAudioFormat();
    format->setSampleRate(48000);//采样率
    format->setChannelCount(2);//通道
    format->setSampleSize(32);//样本长度
    format->setByteOrder(QAudioFormat::LittleEndian);//设置端字节序：小端序
    format->setSampleType(QAudioFormat::Float);//存储样本数据类型
    format->setCodec("audio/pcm");//这个用来设置编码器为audio/pcm，该编码器为所有平台支
    buffer = new MyBuffer();
    QAudioDeviceInfo infodev = QAudioDeviceInfo::defaultInputDevice();
    if (!infodev.isFormatSupported(*format)) {//不支持打印一下
        printf("not supports");
        return;
    }
    outPut = new QAudioOutput(*format, this);
    input = new QAudioInput(*format, this);
//    MyDevice *device = new MyDevice(this->buffer);
    dev = outPut->start();
    MyThread *t2 = new MyThread(this->buffer);
    ReadThread *t1 = new ReadThread(this->buffer, t2->sockfd, dev);
    t2->start();
    t1->start();
    t = new QTimer(this);
//    connect(t, &QTimer::timeout, [this]() {
//        static int id = 0;
//        struct MsgInfo {
//            int32_t msgSize;
//            int32_t msgType;
//            int32_t msgId;
//        };
//        MsgInfo info;
//        int defaultSize = 4096;
//        char *audioData = nullptr;
//        audioData = new char[defaultSize];
//        int retSize = f->read(audioData, defaultSize);
//        if (retSize == 0) {
//            t->stop();
//        }
//        info.msgSize = retSize;
//        info.msgId = id;
//        info.msgType = 1;
//        id++;
//        auto ret = this->buffer->tcpSocket->write((char *) &info.msgSize, 4);
//        ret = this->buffer->tcpSocket->write((char *) &info.msgType, 4);
//        ret = this->buffer->tcpSocket->write((char *) &info.msgId, 4);
//        ret = this->buffer->tcpSocket->write(audioData, info.msgSize);
//        auto isOK = this->buffer->tcpSocket->flush();
////        bool rec =this->buffer->tcpSocket->waitForBytesWritten();
//    });
//    t->start(25);
//    task=new MyThread(this->buffer);
//    task->start();


//    input->start(device);
//    outPut->start(device);
//    while (true) {
//        static int id = 0;
//        struct MsgInfo {
//            int32_t msgSize;
//            int32_t msgType;
//            int32_t msgId;
//        };
//        MsgInfo info;
//        int defaultSize = 4096;
//        char *audioData = nullptr;
//        audioData = new char[defaultSize];
//        int retSize = f->read(audioData, defaultSize);
//        if (retSize == 0) {
//            sleep(1);
//            break;
//        }
//        info.msgSize = retSize;
//        info.msgId = id;
//        info.msgType = 1;
//        id++;
//        auto ret = this->buffer->tcpSocket->write((char *) &info.msgSize, 4);
//        ret = this->buffer->tcpSocket->write((char *) &info.msgType, 4);
//        ret = this->buffer->tcpSocket->write((char *) &info.msgId, 4);
//        ret = this->buffer->tcpSocket->write(audioData, info.msgSize);
//        auto isOK = this->buffer->tcpSocket->flush();
////        sleep(1);
//        qDebug()<<ret;
//    }
//    connect(this->buffer->tcpSocket, &QTcpSocket::readyRead, [this] {
//
////                data = new char[info.msgSize + 1];
//
//
////                auto ret = this->buffer->tcpSocket->write((char *) &info.msgSize, 4);
////                ret = this->buffer->tcpSocket->write((char *) &info.msgType, 4);
////                ret = this->buffer->tcpSocket->write((char *) &info.msgId, 4);
////                data[info.msgSize] = '\0';
////                ret = this->buffer->tcpSocket->write(data, info.msgSize);
////                qDebug() << data;
////                delete data;
//            }
//    );
//    QDesktopWidget *desk = QApplication::desktop();
//    QRect rect = desk->availableGeometry();
//    desk->showFullScreen();
//    QScreen *priScreen = QGuiApplication::primaryScreen();
//    priScreen->grabWindow(0).save("/home/choumei/CLionProjects/NetClient/resource/test.png");
//    QPixmap map = priScreen->grabWindow(0, 0, 0, rect.width(), rect.height());
//    qDebug() << map.size();
//    this->ui->window->setAttribute(Qt::WA_TransparentForMouseEvents, true);
//    auto ret2 = map.scaled(this->ui->window->width(), this->ui->window->height(), Qt::KeepAspectRatio,
//                           Qt::SmoothTransformation);
////    this->ui->window->resize(map.width(), map.height());
//    this->ui->window->setPixmap(ret2);
}

Widget::~Widget() {
    delete ui;
}


void Widget::on_pushButton_clicked() {

}

void Widget::add(QAudio::State state) {

}
